<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>js轮播图1</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        @font-face {
            font-family: 'iconfont';
            /* Project id 2526200 */
            src: url('http://at.alicdn.com/t/font_2526200_dra74wlaxnj.woff2?t=1621069802615') format('woff2'), url('http://at.alicdn.com/t/font_2526200_dra74wlaxnj.woff?t=1621069802615') format('woff'), url('http://at.alicdn.com/t/font_2526200_dra74wlaxnj.ttf?t=1621069802615') format('truetype');
        }
        /* body {
            position: relative;
        } */
        
        a {
            text-decoration: none;
            display: block;
        }
        
        li {
            list-style-type: none;
        }
        
        .wrap {
            width: 800px;
            height: 500px;
            margin: 200px auto;
            overflow: hidden;
            position: relative;
        }
        
        .wrap .list {
            width: 100%;
            height: 100%;
            position: relative;
        }
        
        .wrap .list .item {
            position: absolute;
            opacity: 0;
            transition: all 1s;
        }
        
        .wrap .list .item>a>img {
            width: 800px;
            height: 500px;
        }
        
        .wrap .btn {
            width: 40px;
            height: 60px;
            position: absolute;
            top: 50%;
            color: #fff;
            font-weight: 800;
            background-color: rgba(0, 0, 0, .1);
            transform: translate(0, -50%);
            font-family: 'iconfont';
            font-size: 20px;
            border: none;
            z-index: 1;
            text-align: center;
            cursor: pointer;
        }
        
        .wrap #goPre {
            left: -5px;
            ;
            border-radius: 0 100% 100% 0;
        }
        
        .wrap #goNext {
            right: -5px;
            ;
            border-radius: 100% 0 0 100%;
        }
        
        .wrap .list .active {
            z-index: 1;
            opacity: 1;
        }
        
        .wrap .pointList {
            position: absolute;
            bottom: 15px;
            left: 50%;
            transform: translate(-50%, 0);
            z-index: 1;
        }
        
        .wrap .pointList .point {
            width: 10px;
            height: 10px;
            border-radius: 50%;
            display: inline-block;
            margin-right: 12px;
            border: 2px solid #fff;
            background-color: #fff;
            cursor: pointer;
            transition: all 1s;
        }
        
        .wrap .pointList .current {
            background-color: coral;
            transform: scale(1.5);
        }
    </style>
</head>

<body>
    <div class="wrap">
        <ul class="list">

            <li class="item">
                <a><img src="images/t20.jpg" alt=""></a>
            </li>
            <li class="item">
                <a><img src="images/t21.jpg" alt=""></a>
            </li>
            <li class="item">
                <a><img src="images/t22.jpg" alt=""></a>
            </li>
            <li class="item">
                <a><img src="images/t23.jpg" alt=""></a>
            </li>
            <li class="item">
                <a><img src="images/t19.jpg" alt=""></a>
            </li>
        </ul>
        <button class="btn" id="goPre">&#xe667;</button>
        <button class="btn" id="goNext">&#xe610;</button>
        <ul class="pointList">
            <li class="point"></li>
            <li class="point"></li>
            <li class="point"></li>
            <li class="point"></li>
            <li class="point"></li>
        </ul>
    </div>

    <script>
        var items = document.querySelectorAll('.item');

        var goPreBtn = document.querySelector('#goPre');

        var goNextBtn = document.querySelector('#goNext');

        var pointLists = document.querySelectorAll('.point');

        var wrap = document.querySelector('.wrap');

        var index = 0;
        var time = 0;
        var timer = null;
        var lock = true; //节流阀
        function clearIndex() {
            for (var i = 0; i < items.length; i++) {
                items[i].className = 'item';
                pointLists[i].className = 'point';
            }
        }

        function goIndex() {
            clearIndex();
            items[index].className = 'item active';
            pointLists[index].className = 'point current';
        }
        goIndex();

        function goNext() {
            index++;
            if (index > 4) {
                index = 0;
            }
            goIndex();
        }

        function goPre() {
            index--;
            if (index < 0) {
                index = 4;
            }
            goIndex();
        }

        function rest() {
            time++;

            if (time == 20) {
                time = 0;
                goNext();
            }
        }

        function openLock() {
            lock = true;
        }

        goNextBtn.addEventListener('click', function() {
            if (!lock) return; //是false直接结束函数
            goNext();
            time = 0;
            lock = false;
            //延迟1s，让transition的过渡时间过渡完再打开
            setTimeout(openLock, 1000)

        });
        goPreBtn.addEventListener('click', function() {
            if (!lock) return;
            goPre();
            lock = false;
            time = 0;

            setTimeout(openLock, 1000);
        });

        for (var j = 0; j < pointLists.length; j++) {
            pointLists[j].setAttribute('pointIndex', j);

            pointLists[j].addEventListener('click', function() {
                var pIndex = this.getAttribute('pointIndex');
                index = pIndex;
                goIndex();
                time = 0;
            })
        }


        wrap.addEventListener('mouseover', function() {
            clearInterval(timer);
            time = 0;
        })
        wrap.addEventListener('mouseout', function() {
            time = 0;
            timer = setInterval(rest, 150);
        })
        timer = setInterval(rest, 150)
    </script>
</body>

</html>